<?php
/**
 * 编辑管理员
 *
 * @author Dracowyn
 * @since 2023-11-17 16:12
 */

// 引入配置文件
include_once('config/config.php');
$loginAuth = check_auth();

$id = $_GET['id'] ?? 0;

$adminSql = "SELECT * FROM `pre_admin` WHERE `id` = '$id'";
$adminData = find($adminSql);
if (!$adminData) {
	showMsg('管理员不存在');
	exit();
}

if ($_POST) {
	// 接收数据
	$username = trim($_POST['username']);
	$password = trim($_POST['password']);
	$rePassword = trim($_POST['rePassword']);

	// 验证数据
	if (empty($username)) {
		showMsg('用户名不能为空');
		exit();
	}

	if (!empty($password)) {
		if ($password != $rePassword) {
			showMsg('两次密码不一致');
			exit();
		} else {
			// 密码加密
			$salt = randStr();
			$password = md5((md5($password)) . $salt);
		}
	}

	// 判断用户名是否存在
	$sql = "select username from pre_admin where username = '$username' and id != '$id'";
	$findUser = all($sql);
	if ($findUser) {
		showMsg('用户名已存在');
		exit();
	}

	// 更新数据
	$data = [
		'username' => $username,
	];

	if (!empty($password)) {
		$data['password'] = $password;
		$data['salt'] = $salt;
	}

	if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
		$res = upload('avatar');
		if ($res['code'] === 0) {
			showMsg($res['msg']);
		}
		$data['avatar'] = $res['data']['filename'];
	}

	$result = update('admin', $data, "`id` = '$id'");

	if ($result['code'] === 0) {
		@is_file('./upload/' . $data['avatar']) && @unlink('./upload/' . $data['avatar']);
		showMsg($result['msg']);
		exit();
	} else {
		if (isset($data['avatar']) && $_FILES['avatar']['size'] > 0) {
			@is_file('./upload/' . $adminData['avatar']) && @unlink('./upload/' . $adminData['avatar']);
		}
		showMsg('更新成功', 'adminList.php');
	}
}

?>

<!DOCTYPE html>
<html lang="zh">

<head>
	<?php include_once('common/meta.php'); ?>
    <link rel="stylesheet" href="assets/city-picker/city-picker.css">
    <title>编辑管理员信息</title>
</head>

<style>
    #upload {
        width: 200px;
        height: 200px;
        cursor: pointer;
    }
</style>

<body>
<?php include_once('common/header.php'); ?>
<?php include_once('common/menu.php'); ?>
<div class="content">
    <div class="header">
        <h1 class="page-title">编辑管理员</h1>
    </div>
    <ul class="breadcrumb">
        <li><a href="index.php">首页</a> <span class="divider">/</span></li>
        <li><a href="adminList.php">管理员管理</a> <span class="divider">/</span></li>
        <li class="active">编辑管理员</li>
    </ul>

    <div class="container-fluid">
        <div class="row-fluid">

            <div class="btn-toolbar">
                <button class="btn btn-primary" onClick="location='adminList.php'"><i class="icon-list"></i>管理员列表
                </button>
                <div class="btn-group">
                </div>
            </div>

            <div class="well">
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane active in" id="home">
                        <form method="post" enctype="multipart/form-data">
                            <label for="username">用户名</label>
                            <input type="text" name="username" id="username" placeholder="请输入用户名" required
                                   class="input-xxlarge"
                                   value="<?php echo $adminData['username']; ?>">
                            <label for="password">密码（留空则不修改密码）</label>
                            <input type="password" name="password" id="password" class="input-xxlarge"
                                   placeholder="请输入密码">
                            <label for="rePassword">确认密码</label>
                            <input type="password" name="rePassword" id="rePassword" class="input-xxlarge"
                                   placeholder="请输入确认密码">
                            <label>头像</label>
                            <input type="file" name="avatar" id="avatar" hidden class="input-xxlarge">
                            <img src="<?php echo is_file('./upload/' . $adminData['avatar']) ? '/upload/' . $adminData['avatar'] : 'assets/images/upload.png' ?>"
                                 id="upload" onclick="avatar.click();" alt="">
                            <label></label>
                            <input class="btn btn-primary" type="submit" value="提交"/>
                        </form>
                    </div>
                </div>
            </div>
			<?php include_once('common/footer.php'); ?>
        </div>
    </div>
</div>
</body>
<?php include_once 'common/script.php'; ?>
<script>
    $('#avatar').change(function () {
        let file = $(this)[0].files[0];
        if (file) {
            let reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function () {
                $('#upload').attr('src', reader.result);
            }
        }
    })
</script>
</html>